﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ADC.DAL;
using log4net;

namespace ADC.BL
{
    public class ProjectTypeFactory
    {
        /// <summary>
        /// Get all visible
        /// </summary>
        /// <returns></returns>
        public static List<ProjectType> GetAll()
        {
            var db = new dbADCEntities();
            return db.ProjectTypes.ToList();
        }

        public static ProjectType GetByID(int projectTypeID)
        {
            var db = new dbADCEntities();
            return db.ProjectTypes.Where(g => g.ProjectTypeID == projectTypeID).FirstOrDefault();
        }

        // Update Programe
        /// </summary>
        /// <param name="programe"></param>
        /// <returns></returns>
        public static int Update(ProjectType projectType)
        {
            try
            {
                var db = new dbADCEntities();
                var objPrjectType = new ProjectType();
                objPrjectType = db.ProjectTypes.Where(g => g.ProjectTypeID == projectType.ProjectTypeID).FirstOrDefault();
                objPrjectType.Title = projectType.Title;
                objPrjectType.ModifiedDate = DateTime.Now;                
                if (!string.IsNullOrEmpty(projectType.ImagePath))
                {
                    objPrjectType.ImagePath = projectType.ImagePath;
                }

                return db.SaveChanges();
            }
            catch (Exception)
            {
                // Log here
                throw;
            }
        }

        public static ProjectType Insert(ProjectType projectType)
        {
            var db = new dbADCEntities();
            db.AddToProjectTypes((projectType));
            db.SaveChanges();
            return projectType;
        }

        public static int Delete(int projectTypeID)
        {
            var db = new dbADCEntities();
            // Delete projectType
            var projectType = db.ProjectTypes.FirstOrDefault(g => g.ProjectTypeID.Equals(projectTypeID));
            if (projectType != null) db.DeleteObject(projectType);
            return db.SaveChanges();
        }

        public static int CountProject(int projectTypeID)
        {
            try
            {
                var db = new dbADCEntities();
                var objProject = db.Projects.Where(g => g.ProjectTypeID == projectTypeID).ToList();
                return objProject.Count;
            }
            catch (Exception)
            {
                return -1;
                // Log here
                throw;
            }
        }


    }
}
